﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IBMPBLL
{
    /// <summary>
    /// 实体描述信息类
    /// </summary>
    public class EntityDescription
    {
        /// <summary>
        /// 实体类的全名称
        /// </summary>
        public string ClassFullName { get; set; }
        /// <summary>
        /// 数据表名称
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 表描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 包含的属性描述
        /// </summary>
        public List<PropertyDescription> Propertys { get; set; }

        /// <summary>
        /// 属性描述信息类
        /// </summary>
        public class PropertyDescription
        {
            /// <summary>
            /// 字段名称
            /// </summary>
            public string FieldName { get; set; }
            /// <summary>
            /// 字段描述
            /// </summary>
            public string Description { get; set; }
        }
    }

    /// <summary>
    /// 实体描述信息配置类
    /// </summary>
    public class EntityDescriptionConfig
    {
        /// <summary>
        /// 从实体描述文件中获取实体类描述信息
        /// </summary>
        /// <param name="configFileName">实体描述文件</param>
        /// <returns>实体类描述信息</returns>
        public List<EntityDescription> GetEntityDescription(string configFileName)
        {
            XElement xml = XElement.Load(configFileName);

            var s = from b in xml.Descendants("Entity")
                    select new EntityDescription {
                        ClassFullName = b.Attribute("Class").Value, 
                        TableName = b.Attribute("TableName").Value ,
                        Description = b.Attribute("Description").Value 
                    };
            //必须先求值，否则LINQ的延迟求值特性会让 Propertys 属性处理无效。
            s = s.ToList<EntityDescription>();

            foreach (var i in s)
            {
                var a = from p in xml.Descendants("Property")
                        where p.Parent.Attribute("Class").Value == i.ClassFullName
                        select new EntityDescription.PropertyDescription {
                            FieldName = p.Attribute("FieldName").Value, 
                            Description = p.Attribute("Description").Value 
                        };
                i.Propertys = a.ToList<EntityDescription.PropertyDescription>();
            }
            return s.ToList<EntityDescription>();


        }

        /// <summary>
        /// 通过读取应用程序配置节配置信息 EntityDescriptionFile，从实体描述文件中获取实体类描述信息。
        /// </summary>
        /// <returns>实体描述信息类</returns>
        public static  List<EntityDescription> GetEntityDescriptions(string filepath)
        {
            string configFileName = filepath;//System.Configuration.ConfigurationManager.AppSettings["EntityDescriptionFile"];
            EntityDescriptionConfig config = new EntityDescriptionConfig();
            return config.GetEntityDescription(configFileName);
        }
    
    }
}
